///////////////////////////////////////////////////////
//Generating Selection Measures for Table 9
///////////////////////////////////////////////////////
use "[Folder]\Panel_Ind.dta" , clear

keep LopNr_PersonNr  Kon Fodelsear ForvInk Sun2000niva year
gen Age = year - Fodelsear
drop if Age<25
drop if Age>59

merge m:1 LopNr_PersonNr using "[Folder]\TimeInv_Ind.dta", keepusing(FodelseLandnamn InvAr UtvAr )
drop if _merge==2
drop _merge
drop if FodelseLandnamn==107

// Years of Education. Use only for experience
nsplit Sun2000niva, digits(1 1 1) generate(SunD1 SunD2 SunD3)
gen Educ_Ar = 0
replace Educ_Ar = 3 if SunD1==9
replace Educ_Ar = 5 if SunD1==1
replace Educ_Ar = 9 if SunD1==2
replace Educ_Ar = 10 if SunD1==3 & SunD2==1
replace Educ_Ar = 11 if SunD1==3 & SunD2==2
replace Educ_Ar = 12 if SunD1==3 & SunD2==3
replace Educ_Ar = 13 if SunD1==4 & SunD2==1
replace Educ_Ar = 14 if SunD1==5 & SunD2==2
replace Educ_Ar = 15 if SunD1==5 & SunD2==3
replace Educ_Ar = 16 if SunD1==5 & SunD2==4
replace Educ_Ar = 17 if SunD1==5 & SunD2==5
replace Educ_Ar = 18 if SunD1==6 & SunD2==0
replace Educ_Ar = 18 if SunD1==6 & SunD2==2
replace Educ_Ar = 20 if SunD1==6 & SunD2==4

drop SunD1 SunD2 SunD3

gen EmpStat_100=0
replace EmpStat_100=1 if !missing(ForvInk) & ForvInk>1000 //Annual income of at least 100k SEK, less than half effective minimum wage for a full-time employee. 


collapse (mean)  EmpStat_100 Educ_Ar  (count) TotFreq_Selection=Educ_Ar, by(FodelseLandnamn Kon)

drop if missing(FodelseLandnamn)

reshape wide  EmpStat_100 Educ_Ar TotFreq_Selection,  i(FodelseLandnamn) j(Kon)
rename Educ_Ar2 Educ_Ar_Fem
rename Educ_Ar1 Educ_Ar
rename EmpStat_1001 EmpStat_100
rename EmpStat_1002 EmpStat_100_Fem 
rename TotFreq_Selection1 TotFreq_Selection
rename TotFreq_Selection2 TotFreq_Selection_Fem

gen Educ_Ar_Tot = (TotFreq_Selection*Educ_Ar + TotFreq_Selection_Fem*Educ_Ar_Fem)/(TotFreq_Selection+TotFreq_Selection_Fem)
drop if missing( FodelseLandnamn)
save "[Output Folder]\Selection_Collapse.dta"


//Re-migration
clear all
use "[Folder]\Panel_Ind.dta" 

keep LopNr_PersonNr  Kon Fodelsear Sun2000niva ForvInk Kommun year

merge m:1 LopNr_PersonNr using "[Folder]\TimeInv_Ind.dta", keepusing(FodelseLandnamn InvAr UtvAr )
drop if _merge==2
drop _merge



// Years of Education. Use only for experience
nsplit Sun2000niva, digits(1 1 1) generate(SunD1 SunD2 SunD3)
gen Educ_Ar = 0
replace Educ_Ar = 3 if SunD1==9
replace Educ_Ar = 5 if SunD1==1
replace Educ_Ar = 9 if SunD1==2
replace Educ_Ar = 10 if SunD1==3 & SunD2==1
replace Educ_Ar = 11 if SunD1==3 & SunD2==2
replace Educ_Ar = 12 if SunD1==3 & SunD2==3
replace Educ_Ar = 13 if SunD1==4 & SunD2==1
replace Educ_Ar = 14 if SunD1==5 & SunD2==2
replace Educ_Ar = 15 if SunD1==5 & SunD2==3
replace Educ_Ar = 16 if SunD1==5 & SunD2==4
replace Educ_Ar = 17 if SunD1==5 & SunD2==5
replace Educ_Ar = 18 if SunD1==6 & SunD2==0
replace Educ_Ar = 18 if SunD1==6 & SunD2==2
replace Educ_Ar = 20 if SunD1==6 & SunD2==4

drop SunD1 SunD2 SunD3




gen Mig=0
replace Mig = 1 if FodelseLandnamn!=107


//Generating COO-FE
forvalues j=1/129 {
	gen CFE_`j' =0
	replace CFE_`j'=1 if FodelseLandnamn==`j'
}



gen Age=year-Fodelsear
gen Exp=Age - Educ_Ar
gen Exp_sq=Exp^2
gen Exp_qu=Exp^3

gen ChildMig = 0
replace ChildMig = 1 if InvAr - Fodelsear < 8 & FodelseLandnamn!=107
gen YouthMig = 0
replace YouthMig = 1 if InvAr - Fodelsear < 16 & InvAr - Fodelsear > 7 & FodelseLandnamn!=107

drop if Age < 25 | Age > 59
drop if  ChildMig==1 | YouthMig==1


gen SweTime=year-InvAr
replace SweTime = 0 if FodelseLandnamn==107
gen SweTime_sq=SweTime^2
gen SweTime_qu=SweTime^3

gen logw=log(ForvInk)

drop CFE_107

//Generating COO-specific returns to time-in-sweden 
foreach var of varlist CFE_* {
	gen `var'_Time =`var'*SweTime
	gen `var'_Time_sq =`var'*SweTime_sq
	gen `var'_Time_qu =`var'*SweTime_qu
}

gen byte Em = 0
replace Em = 1 if !missing(UtvAr)
 
 //Generating COO-specific emmigrant fixed effect
forvalues j=1/129 {
	gen CFE_`j'_Em =0
	replace CFE_`j'_Em = Em*CFE_`j'
}
 

 //Generating immigration cohort FE
gen Cohort70 =0 
replace Cohort70 =1 if InvAr<1971 & FodelseLandnamn!=107
gen Cohort80 =0 
replace Cohort80 =1 if InvAr>1970 & InvAr<1981 & FodelseLandnamn!=107
gen Cohort90 =0 
replace Cohort90 =1 if InvAr>1980 & InvAr<1991 & FodelseLandnamn!=107
gen Cohort95 =0 
replace Cohort95 =1 if InvAr>1990 & InvAr<1996 & FodelseLandnamn!=107
gen Cohort00 =0 
replace Cohort00 =1 if InvAr>1995 & InvAr<2001 & FodelseLandnamn!=107
gen Cohort05 =0 
replace Cohort05 =1 if InvAr>2000 & InvAr<2006 & FodelseLandnamn!=107
gen Cohort10 =0 
replace Cohort10 =1 if InvAr>2005 & InvAr<2011 & FodelseLandnamn!=107
gen Cohort15 =0 
replace Cohort15 =1 if InvAr>2010 & InvAr<2016 & FodelseLandnamn!=107

 

cd "[Output Folder]\"
reg logw Exp Exp_sq Exp_qu Educ_Ar i.year ib180.Kommun CFE_* Cohort* if Kon==1 
parmest, format(estimate min95 max95  %8.2f p %8.1e) saving(CFE_MA_RM, replace)

use "[Output Folder]\CFE_MA_RM.dta" , clear

//Extract COO emmigrant FE
gen CFE=strpos(parm,"CFE")
drop if CFE==0 
drop CFE
gen omitted=strpos(parm,"o.")
drop if omitted==1
drop omitted
gen CFE_Em=strpos(parm,"Em")
drop if CFE_Em==0
gen part=regexr(parm,"_Em","")
replace part=regexr(part,"CFE_","")
destring part, replace
rename part FodelseLandnamn
keep FodelseLandnamn estimate  
rename estimate CFE_Em

merge m:1 FodelseLandnamn using "[Data Folder]\MacroData_ToMona_v1"

drop if _merge==2
drop _merge

merge 1:1 FodelseLandnamn using "[Output Folder]\Selection_Collapse.dta"
drop if _merge==2
drop _merge


merge 1:1 FodelseLandnamn using "[Output Folder]\HC_v1_clean.dta"
drop if _merge==2
drop _merge


//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Point estimates for Table 9, Column 2
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
reg CFE_Em TR [w=TotFreq_Selection] if WVInd==1  & FodelseLandnamn!=107 
reg CFE_Em loggdp05 [w=TotFreq_Selection] if WVInd==1  & FodelseLandnamn!=107 
reg CFE_Em HC [w=TotFreq_Selection] if WVInd==1   & FodelseLandnamn!=107 

//Results in paper for re-migrants uses data from 2003-2014 (as earnings profiles potentially need longer time series) rather than 2008-2014 used in most of the paper. However, the difference to using 2008-2014 is negligible for the emmigrant fixed effects. 




use "[Output Folder]\Selection_Collapse.dta", clear
merge 1:1 FodelseLandnamn using "[Data Folder]\MacroData_ToMona_v1"
drop if _merge==2
drop _merge

merge 1:1 FodelseLandnamn using "[Data Folder]\Output\HC_v1_clean.dta"
drop if _merge==2
drop _merge

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//Point estimates for Table 9, Column 3
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////
reg EmpStat_100 TR [w=TotFreq_Selection] if WVInd==1   & FodelseLandnamn!=107 
reg EmpStat_100 loggdp05 [w=TotFreq_Selection] if WVInd==1  & FodelseLandnamn!=107 
reg EmpStat_100 HC [w=TotFreq_Selection] if WVInd==1 & FodelseLandnamn!=107 



gen RelEd=Educ_Ar/yr_sch_15992005
gen RelEd_Tot=Educ_Ar_Tot/yr_sch_15992005


 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //Point estimates for Table 9, Column 1
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
 //////////////////////////////////////////////////////////////////////////////////////////////////////////////
reg RelEd TR [w=TotFreq_Selection] if WVInd==1  & FodelseLandnamn!=107 
reg RelEd loggdp05 [w=TotFreq_Selection] if WVInd==1 & FodelseLandnamn!=107 
reg RelEd HC [w=TotFreq_Selection] if WVInd==1  & FodelseLandnamn!=107 
 
